﻿#ifndef JCURVE_WIDGET_H
#define JCURVE_WIDGET_H

#include "jcurve_global.h"
#include <QWidget>
#include <QDateTime>

// - class JCurveWidget -

class QwtPlotShapeItem;
class JCurve;
class JCurveWidgetPrivate;
class JSiftAreaEditor;
class QwtPlot;

class JCURVE_EXPORT JCurveWidget : public QWidget
{
    Q_OBJECT
    Q_PROPERTY(bool enableDbClick READ enableDbClick WRITE setEnableDbClick NOTIFY enableDbClickChanged)
    Q_PROPERTY(bool baseLineEnabled READ baseLineEnabled WRITE setBaseLineEnabled NOTIFY baseLineEnabledChanged)
public:
    enum AxisXYType
    {
        AxisXY_Invalid = -1,
        AxisXY_Time_Time,
        AxisXY_Time_Value,
        AxisXY_Value_Time,
        AxisXY_Value_Value
    };
    enum CurveStyle {
        CurveStyle_Invalid = -1,
        CurveStyle_Dots = 0,
        CurveStyle_Lines,
        CurveStyle_Fitted
    };

    explicit JCurveWidget(QWidget *parent = 0, bool zoom = false);
    explicit JCurveWidget(const QString &title, QWidget *parent = 0, bool zoom = false);
    ~JCurveWidget();

    JCurve *addCurve(const QString &title);
    JCurve *addCurve(const QString &title, const QPen &pen);
    JCurve *addCurve(const QString &title, const QPen &pen,
                     const QVector<QPointF> &samples);
    void removeCurve(JCurve *curve);
    void removeCurve(int index);
    void clearCurve();

    void autoScale();

    QString title() const;
    void setTitle(const QString &text);

    QString curveTitle(int index) const;
    bool setCurveTitle(int index, const QString &text);
    JCurve *curve(int index) const;
    QVector<JCurve *> &curves();

    bool enableDbClick() const;
    bool baseLineVisible() const;
    bool baseLineEnabled() const;
    bool curveStyleVisible() const;
    bool editEnabled() const;
    bool exportVisible() const;

    void setNormalScale();

    AxisXYType axisXYType() const;
    void setAxisXYType(AxisXYType type);

    bool clearData(int index = -1);
    bool updateData(int index, const QVector<QPointF> &data);
    bool updateData(JCurve *curve, const QVector<QPointF> &data);
    bool updateData(int index, const QList<QPair<QTime, qreal> > &data);
    bool updateData(JCurve *curve, const QList<QPair<QTime, qreal> > &data);

    static QColor randColor(qreal opacity = 1.0);

    JSiftAreaEditor *siftAreaEditor();

    //
    QList<QRectF> siftAreas() const;

    //
    void setBaseLineSamples(const QList<QVector<QPointF> > &groupsData);

    //
    void clearHotSpot();
    void addHotSpot(const QRectF &range, bool notify = false);

    void setOptionWidgetVisible(bool visible);
    void setZoomMode(bool checked);
    void setSiftMode(bool checked);
    void setZoomModeVisible(bool visible);
    void setSiftModeVisible(bool visible);
    void setBaseLineVisible(bool visible);
    void setCurveStyleVisible(bool visible);

    void setCurveStyle(CurveStyle curveStyle);

    void setLegendVisible(bool visible);

    void setEditWidgetVisible(bool visible);

    void setExportVisible(bool visible);

Q_SIGNALS:
    void enableDbClickChanged(bool enable);
    void baseLineEnabledChanged(bool enabled);
    void doubleClicked(bool checked);
    void selectedChanged(int index, JCurve *curves);
    void siftAreaChanged(int index, const QRectF &rect);
    void editClicked();
    void timeBandApply(const QTime &minimum, const QTime &maximum, bool retain);

public Q_SLOTS:
    void setEnableDbClick(bool enable);
    void setBaseLineEnabled(bool enabled);
    void setEditEnabled(bool enabled);

protected:
    void mouseDoubleClickEvent(QMouseEvent *event);

private:
    J_DECLARE_PRIVATE(JCurveWidget)
};

#endif // JCURVE_WIDGET_H
